/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.model;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.model.entity.Config;
import br.com.jr2tecnologia.model.persistence.DAOException;
import br.com.jr2tecnologia.model.persistence.DAOFactory;
import br.com.jr2tecnologia.model.repository.ConfigRepository;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author ronildo
 */

@Stateless
public class ConfigPersistence implements ConfigPersistenceLocal{

    private static final Logger logger = LoggerFactory.getLogger();

    @PersistenceContext
    private EntityManager em;
    private ConfigRepository configRepository;

    @PostConstruct
    public void initialize(){
        configRepository = DAOFactory.getConfigRepository(em);
    }

    public void persist(Config config){
        configRepository.persist(config);
    }

    public void update(Config config){
        configRepository.update(config);
    }

    public void remove(Config config){
        configRepository.remove(config, config.getPkConfig());
    }

    public Config findById(Config config){
        Config entity = null;
        try{
            entity = configRepository.findById(config.getPkConfig());
        }catch(DAOException e){
            logger.log(Level.SEVERE, "canot find login for this name: " + config.getPkConfig(), e);
        }
        return entity;
    }

    public Config findConfigByName(String name) throws PersistenceUnitException{
        Config entity = null;
        try{
            entity = configRepository.findByName(name);
        }catch(DAOException ex){
            logger.severe("cannot find Config this name: " + name);
            throw new PersistenceUnitException(ex);
        }
        return entity;
    }

    public List<Config> list(Config config){
        try{
            return configRepository.list(config);
        }catch(DAOException e){
            logger.log(Level.SEVERE, "canot list config", e);
            throw new DAOException(e);
        }
    }

    public List<Config> listAll(){
        try{
            return configRepository.listAll();
        }catch(DAOException e){
            logger.log(Level.SEVERE, "canot list config", e);
            throw new DAOException(e);
        }
    }
}